// Square Control.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "SDL/include/SDL.h"  
#include "SDL/SDL_GfxPrimitives/SDL_gfxPrimitives.h"
#include <windows.h>  
#include <stdio.h>
#pragma comment (lib, "SDL/lib/SDL.lib")
#pragma comment (lib, "SDL/SDL_GfxPrimitives/SDL_GfxPrimitives_Static.lib")

SDL_Surface *mScreen;

void initSDLGraphics();
void shutdownGraphics();
void startKeyboardEventHandling();
void drawSquare(int xPos, int yPos);

int _tmain(int argc, _TCHAR* argv[])
{
	initSDLGraphics();

	startKeyboardEventHandling();

	shutdownGraphics();

}

void startKeyboardEventHandling(){
	SDL_Event event;
	 int quit = 0;

        /* Enable Unicode translation */
        SDL_EnableUNICODE( 1 );
int xPos = 100, yPos = 100, xVel = 0, yVel = 0;


  while( !quit ){
	drawSquare(xPos,yPos);	  
	
            /* Poll for events */
            while( SDL_PollEvent( &event ) ){
                
                switch( event.type ){
                    /* Keyboard event */
                    /* Pass the event data onto PrintKeyInfo() */
                    case SDL_KEYDOWN:
						switch(event.key.keysym.sym){
							case SDLK_w:
							case SDLK_UP:
								yVel = -1;
								break;
							case SDLK_a:
							case SDLK_LEFT:
								xVel = -1;
								break;
							case SDLK_s:
							case SDLK_DOWN:
								yVel = 1;
								break;
							case SDLK_d:
							case SDLK_RIGHT:
								xVel = 1;
								break;
						}
                        break;
					case SDL_KEYUP:
						switch(event.key.keysym.sym){
							case SDLK_w:
							case SDLK_UP:
								if(yVel <0 ) yVel = 0;
								break;
							case SDLK_a:
							case SDLK_LEFT:
								if(xVel < 0 ) xVel = 0;
								break;
							case SDLK_s:
							case SDLK_DOWN:
								if(yVel >0 ) yVel = 0;
								break;
							case SDLK_d:
							case SDLK_RIGHT:
								if(xVel >0) xVel = 0;
								break;
							case SDLK_ESCAPE:
								quit = 1;
								break;
						}
                        break;

                    /* SDL_QUIT event (window close) */
                    case SDL_QUIT:
                        quit = 1;
                        break;

                    default:
                        break;
                }
				
            }
		
			/* Update events */
			xPos += xVel;
			yPos += yVel;
        }


}

void drawSquare(int xPos, int yPos){
	boxColor (mScreen, 0, 0, mScreen->w - 1, mScreen->h - 1, 0x000000ff);
	boxColor (mScreen, xPos, yPos, xPos+20, yPos+20, 0xff0000ff);
	SDL_Flip(mScreen);
}

void initSDLGraphics(){
	

	printf("Initializing SDL.\n");
    
    /* Initialize defaults, Video and Audio */
    if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) { 
        printf("Could not initialize SDL: %s.\n", SDL_GetError());
        exit(-1);
	}
	    
    /*
     * Initialize the display in a 800x600 8-bit palettized mode,
     * requesting a software surface
     */
	mScreen = SDL_SetVideoMode(800, 600, 8, SDL_SWSURFACE|SDL_ANYFORMAT);

    if ( mScreen == NULL ) {
        fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",
                        SDL_GetError());
        exit(1);
	}

    printf("SDL initialized.\n");
}

void shutdownGraphics(){
	printf("Quiting SDL.\n");

        /* Clean up on exit */
    atexit(SDL_Quit);


    printf("Quiting....\n");

    exit(0);
}


